package net.xju3_4.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import net.xju3_4.common.framework.simplemvc.RequestMapping;
import net.xju3_4.common.utils.JSONUtil;
import net.xju3_4.common.utils.PageInfo;
import net.xju3_4.common.utils.Result;
import net.xju3_4.entity.MerchantEntity;
import net.xju3_4.entity.UserEntity;
import net.xju3_4.service.AdminUserService;
import net.xju3_4.service.impl.AdminUserServiceImpl;

import java.io.UnsupportedEncodingException;

@RequestMapping("/api/admin/user")
public class AdminUserController {
    private AdminUserService adminUserService=new AdminUserServiceImpl();
    @RequestMapping("/pagelist.do")
    public void pagelist(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        String pageNoStr = req.getParameter("pageNo");
        String pageSizeStr = req.getParameter("pageSize");
        Integer pageNo = pageNoStr == null || pageSizeStr.isEmpty() ? 1 : Integer.parseInt(pageNoStr);
        Integer pageSize =pageNoStr==null || pageSizeStr.isEmpty() ? 10 : Integer.parseInt(pageSizeStr);
        String username=req.getParameter("username");
        String realName=req.getParameter("realName");
        String phone=req.getParameter("phone");
        UserEntity user = new UserEntity();
        if(username != null) user.setUsername(username);
        if(realName != null) user.setRealName(realName);
        if(phone != null) user.setPhone(phone);
        // 创建服务实例
        PageInfo<UserEntity> pageInfo = adminUserService.pagelist(pageNo, pageSize,user);
        if(pageInfo != null)
            JSONUtil.printByJSON(resp, Result.success(pageInfo,"获取成功"));
        else
            JSONUtil.printByJSON(resp, Result.fail("获取失败"));
    }
    @RequestMapping("/forbid.do")
    public void forbid(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        String idStr = req.getParameter("id");
        Integer id = Integer.parseInt(idStr);
        if(adminUserService.forbid(id)>0)
            JSONUtil.printByJSON(resp,Result.success("禁用成功"));
        else
            JSONUtil.printByJSON(resp,Result.fail("禁用失败"));
    }
    @RequestMapping("/activate.do")
    public void activate(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        String idStr = req.getParameter("id");
        Integer id = Integer.parseInt(idStr);
        if(adminUserService.activate(id)>0)
            JSONUtil.printByJSON(resp,Result.success("激活成功"));
        else
            JSONUtil.printByJSON(resp,Result.fail("激活失败"));
    }
    @RequestMapping("/delete.do")
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        String idStr = req.getParameter("id");
        Integer id = Integer.parseInt(idStr);
        if(adminUserService.delete(id)>0)
            JSONUtil.printByJSON(resp,Result.success("删除成功"));
        else
            JSONUtil.printByJSON(resp,Result.fail("删除失败"));
    }

}
